<template>
  <!--  工序进度报表-->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" label-width="auto" class="demo-form-inline"
               @submit.native.prevent="getList()">
        <el-form-item style="display: none;">
          <button type="submit"></button>
        </el-form-item>
        <el-row>
          <el-col :span="5">
            <el-form-item label="工厂/车间" prop="organizationId">
              <el-tree-select v-model="searchParam.organizationId" filterable
                              :data="workRoomList" check-strictly style="width: 200px"
                              :props="{ children: 'sonList', label: 'orgName', value: 'id' }"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="生产计划号">
              <el-input v-model="searchParam.planNo" placeholder="请输入生产计划号" clearable style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="计划状态">
              <el-select v-model="searchParam.status" placeholder="请选择计划状态" clearable style="width: 200px;">
                <el-option v-for="item in statusData" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="计划属性">
              <el-select v-model="searchParam.property" placeholder="请选择计划属性" clearable style="width: 200px">
                <el-option label="返工" :value="1"/>
                <el-option label="正常" :value="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="4">-->
          <!--            <el-form-item label="设备">-->
          <!--              <el-select v-model="searchParam.equipmentId" placeholder="请选择设备" clearable filterable remote-->
          <!--                reserve-keyword :remote-method="remoteEquipment" style="width: 200px">-->
          <!--                <el-option v-for="item in equipmentOptions" :key="item.id" :label="item.equipmentName"-->
          <!--                  :value="item.id" />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="5">
            <el-form-item label="工序名称">
              <el-input placeholder="输入工序名称" v-model="searchParam.procedureName" style="width: 200px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="产出产品">
              <el-input placeholder="输入产出产品编码" v-model="searchParam.productCode" style="width: 200px;"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="计划时间">
              <el-date-picker v-model="searchParam.times" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
                              range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间"
                              style="width: 515px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
              <el-button :icon="Refresh" @click="resetSearch()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 370px)">
        <el-table-column label="序号" align="center" width="70">
          <template #default="scope">
            <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{
              (pageSearch.current - 1) * pageSearch.pageSize +
              scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="planOrderNo" label="生产计划号" align="center" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="no" label="工序订单号" align="center" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="procedureName" label="工序名称" align="center" min-width="200" show-overflow-tooltip/>
        <el-table-column prop="productCode" label="产品编码" align="center" min-width="250" show-overflow-tooltip/>
        <el-table-column prop="materialNo" label="产出物料编码" align="center" min-width="250" show-overflow-tooltip/>
        <el-table-column prop="orderCount" label="计划数量" width="150" align="center"/>
        <el-table-column prop="reportWorkRecordCount" width="150" label="投料数量" align="center"/>
        <el-table-column prop="reportCount" width="150" label="报工数量" align="center"/>
        <el-table-column prop="finish" width="250" label="完成度百分比" align="center">
          <template #default="scopes">
            <el-progress :text-inside="false" :stroke-width="16" :percentage="scopes.row.finish"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
                       :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
                       :total="pageTotal"
                       @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {computed, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage, ElMessageBox} from 'element-plus';
import axiosInstance from '@/utils/http/api';
import {Delete, Edit, Plus, Refresh, Search} from '@element-plus/icons-vue';
import dayjs from 'dayjs';

onMounted(() => {
  getList();
  getOption();
});


const pageSearch = ref({
  current: 1,
  pageSize: 10
});

const pageTotal = ref(null);//页面总数
const searchParam = ref({
  // times: [
  //   dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
  //   dayjs().format('YYYY-MM-DD HH:mm:ss')
  //
  // ],
});
const teamData = ref([]);//班组
const teamPeopleData = ref([]);//班组人员

const statusData = ref([
  {label: '新建', value: 0},
  {label: '执行中', value: 1},
  {label: '完成', value: 2}
]);

const handleOrganizationChange = (value) => {

  searchParam.value.userId = [];
  searchParam.value.ids = [];
  if (!value) {
    return
  }
  axiosInstance.post('/api/basedataTeam/getAll', {
    organizationId: value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        teamData.value = res.data;
      });

}
const handleTeamChange = (value) => {

  searchParam.value.userId = [];
  if (!value) {
    return
  }
  axiosInstance.post('/api/teamUser/getAll', {
    teamId: value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        teamPeopleData.value = res.data;
      });

}


const paginatedData = ref([]);
const workRoomList = ref([]);//车间


const getOption = () => {
  axiosInstance.post('/api/basedataOrganization/getAll', {})
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        workRoomList.value = res.data;
      });

};

// 获取列表接口
const getList = () => {
  // if (!searchParam.value.userId) {
  //   ElMessage.error('请选择人员');
  //   return
  // }
  // startDate: searchParam.value?.times?.[0],
  //     endDate: searchParam.value?.times?.[1],
  axiosInstance.post('/api/planOrder/planOrderReportChildren', {
    ...searchParam.value,
    ...pageSearch.value
  })
      .then(res => {
        if (res.code !== 200) {
          ElMessage.error(res.message);
          return false;
        }
        paginatedData.value = res.data.records;
        pageTotal.value = res.data.total;
      });
};

const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

// 重置搜索条件
const resetSearch = () => {
  searchParam.value = {};
  pageSearch.value.current = 1;
  // current.value = 1
  getList();
};

</script>
<style scoped></style>
